package service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import dao.*;
import dao.impl.*;
import domian.*;
import service.ReviewService;
import service.UserService;
import utils.Redis;
import utils.ToolUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@SuppressWarnings("all")
public class ReviewServiceImpl implements ReviewService {

    CommentDaoImpl commentDao = new CommentDaoImpl();
    FileDao fileDao = new FileDaoImpl();
    IntegralDao integralDao = new IntegralDaoImpl();
    UserDao userDao = new UserDaoImpl();
    VipDao vipDao = new VipDaoImpl();
    DownInfoDao downInfoDao = new DownInfoDaoImpl();
    InformMessageDao informMessageDao = new InformMessageDaoImpl();
    UserService us = new UserServiceImpl();

    //上传评论
    @Override
    public Result uploadReview(String nickname, Comment comment) {
        Result result = new Result();
        if (comment.getUid() == null || comment.getFid() == null) {
            //用户未登录赋值相应提示信息
            result = ToolUtil.resultStatus(result, ResultStatus.TOURIST_LOGIN);
        } else {
            //查找文件的上传者
            File fileById = fileDao.findFileById(comment.getFid());
            //查找用户是否评论该资源
            Comment comm = commentDao.inquireComment(comment.getUid(), comment.getFid());
            //查找用户是否下载该资源
            String time = downInfoDao.findUpDownDataByUidAndFid(comment.getUid(), comment.getFid());
            if (fileById.getF_userId() == comment.getUid()) {
                //评论人是该资源的上传者赋值相应提示信息
                result = ToolUtil.resultStatus(result, ResultStatus.REVIEW_ERROR2);
            } else if (comm != null) {
                //评论人已评论该资源赋值相应提示信息
                result = ToolUtil.resultStatus(result, ResultStatus.REVIEW_ERROR3);
            } else if (time == null) {
                //您未下载过该资源,无法评论
                result = ToolUtil.resultStatus(result, ResultStatus.REVIEW_ERROR4);
            } else {
                commentDao.addComment(comment);
                //评论成功
                result = ToolUtil.resultStatus(result, ResultStatus.REVIEW_SUCCESS);
                //积分变动
                integralDao.addIntegral(new Integral(comment.getUid(), comment.getFid(), fileById.getF_title(), 2, "评论资源赠送积分", ToolUtil.dateString(new Date())));
                integralDao.addIntegral(new Integral(fileById.getF_userId(), comment.getFid(), fileById.getF_title(), 5, "资源被评论赠送积分", ToolUtil.dateString(new Date())));
                //信息存入通知表
                informMessageDao.addInform(new InformMessage(fileById.getF_userId(), "【评论】" + nickname + "评论了你的资源", ToolUtil.dateString(new Date())));
            }
        }
        return result;
    }

    //展示评论
    @Override
    public Result dataReview(Integer fid, Integer whichPage, Integer rows) {
        Result result = new Result();
        //文件id传过来了
        if (fid != null) {
            Map<String, Object> data = new HashMap<>();
            Integer size = commentDao.pagingComment(fid, null, null).size();
            Integer which = 0;//第几条
            if (rows != null) {
                which = whichPage * rows - rows;
            }
            List<Comment> comments = commentDao.pagingComment(fid, which, rows);
            Integer page;
            if (size % rows == 0) {
                page = size / rows;
            } else {
                page = size / rows + 1;
            }
            data.put("page", page);//一共几页
            data.put("total", size);//一共几条
            data.put("comments", comments);//评论数据
            result.setObj(data);
        }
        return result;
    }

    //展示文件内容
    @Override
    public Result dataFileContent(Integer fid) {
        Result result = new Result();
        //文件id传过来了
        if (fid != null) {
            File fileById = fileDao.findFileById(fid);
            result.setObj(fileById);
        }
        return result;
    }

    //展示积分明细
    @Override
    public Result dataPoints(Integer uid, Integer whichPage, Integer rows) {
        Result result = new Result();
        //用户登录了
        if (uid != null) {
            Map<String, Object> data = new HashMap<>();
            Integer size = integralDao.inquire(uid, null, null).size();
            Integer which = 0;//第几条
            if (rows != null) {
                which = whichPage * rows - rows;
            }
            List<Integral> integrals = integralDao.inquire(uid, which, rows);
            Integer page;
            if (size % rows == 0) {
                page = size / rows;
            } else {
                page = size / rows + 1;
            }
            data.put("page", page);//一共几页
            data.put("integrals", integrals);//积分明细数据
            result.setObj(data);
        }
        return result;
    }

    //开通vip
    @Override
    public Result liberalVip(Integer uid, Integer pointsMinus, long validityMs, Integer pointsAdd, HttpServletRequest req) throws JsonProcessingException {
        Result result = new Result();
        //用户登录了
        if (uid != null) {
            //查询用户的vip信息
            Vip vip = vipDao.inquire(uid);
            if (vip != null && vip.getMaturityTime() == null) {//已经开通了永久vip
                result = ToolUtil.resultStatus(result, ResultStatus.VIP_NO_NEED);
            } else {
                //查询用户信息
                User user = userDao.findUserById(uid);
                //用户当前积分数
                Integer total = user.getTotal();
                if (total < pointsMinus) {
                    //积分不足
                    result = ToolUtil.resultStatus(result, ResultStatus.VIP_ERROR);
                } else {
                    //开通vip
                    user.setTotal(total - pointsMinus + pointsAdd);
                    userDao.updateUser(user);
                    us.userUpdate(user, Redis.getRedisKey(req));
                    //开通时间
                    Date openingDate = new Date();
                    long startTime = openingDate.getTime();
                    Vip v = new Vip(uid, 1, openingDate, null);
                    if (vip == null || vip.getVipStatus() == 0) {//未开通vip
                        //为0的话是永久vip
                        if (validityMs != 0) {
                            //未开通的到期时间
                            long endTime = startTime + validityMs;
                            v.setMaturityTime(new Date(endTime));
                        } else {
                            v.setMaturityTime(null);
                        }
                        vipDao.addVip(v);
                    } else {//已开通vip
                        //为0的话是永久vip
                        if (validityMs != 0) {
                            //已开通的续到期时间
                            Date maturityTime = vip.getMaturityTime();
                            long endTime = maturityTime.getTime() + validityMs;
                            v.setMaturityTime(new Date(endTime));
                        } else {
                            v.setMaturityTime(null);
                        }
                        vipDao.updateVip(v);
                    }
                    result = ToolUtil.resultStatus(result, ResultStatus.VIP_SUCCESS);
                    //积分变动
                    ResultStatus rs1 = ResultStatus.INIEGRAL_CHANGE4;
                    ResultStatus rs2 = ResultStatus.INIEGRAL_CHANGE5;
                    integralDao.addIntegral(new Integral(uid, null, null, -pointsMinus, rs1.getMsg(), ToolUtil.dateString(new Date())));
                    integralDao.addIntegral(new Integral(uid, null, null, pointsAdd, rs2.getMsg(), ToolUtil.dateString(new Date())));
                    //信息存入通知表
                    informMessageDao.addInform(new InformMessage(uid, "【vip】" + user.getNickname() + "开通了vip", ToolUtil.dateString(new Date())));
                }
            }
        }
        return result;
    }
}
